const token=localStorage.getItem("token")

const insertBtn=document.getElementById("insertBtn")
const select1Btn=document.getElementById("select1Btn")
const deleteBtn=document.getElementById("deleteBtn")
const insertAdditionCkb1=document.getElementById("insertAdditionCkb1")

const insertAdditionTable1=document.getElementById("insertAdditionTable1")

var isInsertAddition1=false
var insertAdditionChecked1=[]

insertBtn.addEventListener("click",function(){
    requestInsert()
})
select1Btn.addEventListener("click",function(){
    const data=requestSelect1()
    displaySelect1Data(data)
})
deleteBtn.addEventListener("click",function(){
    requestDelete()
})
insertAdditionCkb1.addEventListener("click",function(){
    if (this.checked) {
        isInsertAddition1=true
        insertAdditionTable1.style.display="table"
        const students = [
            { id: 1, name: 'Alice', age: 20 },
            { id: 2, name: 'Bob', age: 22 },
            { id: 3, name: 'Charlie', age: 23 }
        ];
        //const data=requestInsertAddition1()
        displayInsertAdditionTable1(students)
    } else {
        isInsertAddition1=false
        insertAdditionTable1.style.display="none"
        insertAdditionTable1.innerHTML=''
        insertAdditionChecked1=[]
    }
})


function toggleAccordion(element) {
    var content = element.nextElementSibling;
    content.style.display = content.style.display === 'block' ? 'none' : 'block';
}
async function toggleAccordionAndSelect(element){
    var content = element.nextElementSibling;
    content.style.display = content.style.display === 'block' ? 'none' : 'block';
    console.log("这里请调用请求函数获取到selectData")
    const selectData=await requestSelect()
    displaySelectData(selectData)
}
function displaySelectData(data) {
    const table = document.getElementById('selectTable');
    const tbody = table.querySelector('tbody');
    tbody.innerHTML = '';
    data.forEach(row => {
        // 创建一个表格行
        const tr = document.createElement('tr');

        // 创建列并添加到行中
        for (let key in row) {
            const td = document.createElement('td');
            td.textContent = row[key]; // 将数据内容放入单元格
            tr.appendChild(td); // 将单元格添加到行中
        }

        // 将行添加到 tbody 中
        tbody.appendChild(tr);
    });
}
function displaySelect1Data(data) {
    const table = document.getElementById('select1Table');
    const tbody = table.querySelector('tbody');
    tbody.innerHTML = '';
    data.forEach(row => {
        // 创建一个表格行
        const tr = document.createElement('tr');

        // 创建列并添加到行中
        for (let key in row) {
            const td = document.createElement('td');
            td.textContent = row[key]; // 将数据内容放入单元格
            tr.appendChild(td); // 将单元格添加到行中
        }

        // 将行添加到 tbody 中
        tbody.appendChild(tr);
    });
}
function displayInsertAdditionTable1(data) {
    const tbody = insertAdditionTable1.querySelector("tbody");

    // 清空表格内容，避免重复追加行
    tbody.innerHTML = "";

    data.forEach(function (oneObject) {
        const newRow = document.createElement("tr");

        // 遍历对象属性并添加到表格行
        for (let key in oneObject) {
            const newData = document.createElement("td");
            newData.textContent = oneObject[key];
            newRow.appendChild(newData);
        }

        // 创建复选框单元格
        const checkboxCell = document.createElement("td");
        const checkbox = document.createElement("input");
        checkbox.type = "checkbox";

        // 复选框点击事件
        checkbox.addEventListener("click", function () {
            if (this.checked) {
                // 添加到选中数组
                insertAdditionChecked1.push(oneObject.id);
            } else {
                // 从选中数组中移除
                const index = insertAdditionChecked1.indexOf(oneObject.id);
                if (index !== -1) {
                    insertAdditionChecked1.splice(index, 1);
                }
            }
            console.log(insertAdditionChecked1);
        });

        checkboxCell.appendChild(checkbox);
        newRow.appendChild(checkboxCell);

        // 将新行添加到表格
        tbody.appendChild(newRow);
    });
}
async function requestInsert(){
    const url=`${config.server}`+"/insert/path"
    const data={
        "token":token
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(!response.ok){
        alert("插入成功",getdata)
    }else{
        console.log("插入失败",getdata)
    }
}
async function requestSelect(){
    const url=`${config.server}`+"/api/admin/select/teaching/class"
    const data={
        "token":token
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        location.reload()
        return getdata
    }else{
        console.log("获取数据失败",getdata)
    }
}
async function requestSelect1(){
    const select1Input1=document.getElementById("select1Input1")
    const input1value =select1Input1.value
    const url=`${config.server}`+"/select1/path"
    const data={
        "token":token
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(!response.ok){
        const selectData1=getdata
    }else{
        console.log("获取数据失败",getdata)
        const selectData1=[
            { "id": 4, "name": "David", "age": 40 },
            { "id": 5, "name": "Eve", "age": 45 },
            { "id": 6, "name": "Frank", "age": 50 }
        ]
    }
    return selectData1
}
async function requestDelete(){
    console.log("发送删除请求")
    const deleteInput1 =document.getElementById("deleteInput1")
    const input1Value = deleteInput1.value
    const url=`${config.server}`+"/select1/path"
    const data={
        "token":token
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(!response.ok){
        alert("删除成功",getdata)
    }else{
        console.log("删除失败",getdata)
    }
}
// 在插入新的信息的时候，有时候需要检索一些补充信息
async function requestInsertAddition1(){
    const url=`${config.server}`+"/select/path"
    const data={
        "token":token
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        return getdata
    }else{
        console.log("删除失败",getdata)
        return []
    }
}